package com.jsontoExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.ExcelBuilder;
import com.alibaba.excel.write.ExcelBuilderImpl;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DemoJsonToExcel {

    public static void main(String[] args) {
        String filepath = "E:\\javaproject\\test\\doc\\survey.json";
        JSONArray json = getJson(filepath);
        jsonToExcel(json);

    }
    /**
     * 目标合并列index
     */
    private static Integer targetColumnIndex;
    private static Integer rowIndex;
    private static void jsonToExcel(JSONArray json) {
        String excelFile = "E:\\javaproject\\test\\doc\\test.xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(excelFile, DemoData.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(data(json), writeSheet);
            excelWriter.finish();
        }
    }
    public  static String split = " \r\n | ";
    private static Collection<?> data(JSONArray json) {
        List<DemoData> list = new ArrayList<>(100);
        for (int i = 0; i < json.size(); i++) {
            JSONObject jsonObject = json.getJSONObject(i);
            DemoData data = new DemoData();
            data.setEnName(jsonObject.getString("key"));
            data.setName(jsonObject.getString("name"));
            JSONArray children = jsonObject.getJSONArray("children");
            if (children != null) {
                StringBuilder subName = new StringBuilder();
                StringBuilder subEnName = new StringBuilder();
                StringBuilder subRequired = new StringBuilder();
                StringBuilder subType = new StringBuilder();
                StringBuilder subEnumValue = new StringBuilder();
                for (int j = 0; j < children.size(); j++) {
                    JSONObject subJsonData = children.getJSONObject(j);
                    subName.append(subJsonData.getString("name")).append(split);
                    subEnName.append(subJsonData.getString("key")).append(split);
                    subRequired.append(subJsonData.getString("required")).append(split);
                    subType.append(subJsonData.getString("type")).append(split);
                    JSONArray childrenEnum = subJsonData.getJSONArray("children");
                    if (childrenEnum != null) {
                        StringBuilder subEnumLabe = new StringBuilder();
                        for (int k = 0; k < childrenEnum.size(); k++) {
                            JSONObject subEnumJsonData = childrenEnum.getJSONObject(k);
                            subEnumValue.append("【").append(subJsonData.getString("name")).append("】").append("->")
                                    .append(subEnumJsonData.getString("label"))
                                    .append("->").append(subEnumJsonData.getString("value"))
                                    .append(split);
//                            subEnumValue.append(subEnumJsonData.getString("value")).append(split);
                        }
                    }
                }
                data.setSub_name(subName.toString());
                data.setSub_key(subEnName.toString());
                data.setRequired(subRequired.toString());
                data.setType(subType.toString());
                data.setEnum_label(subEnumValue.toString());
            }
            list.add(data);
        }
        return list;
    }
    private static JSONArray getJson(String filepath) {
        try {
            FileInputStream fin = new FileInputStream(filepath);
            byte[] buff = new byte[1024];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len ;
            while ((len = fin.read(buff))>0){
                bos.write(buff,0,len);
            }
            return JSON.parseArray( bos.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
